//
//  TYLabel.m
//  TYUIKit
//
//  Created by TuyaInc on 2019/5/13.
//

#import "TYLabel.h"

@interface TYLabel ()

@property (nonatomic) BOOL ty_needRefreshParagraphStyle;

@end

@implementation TYLabel

#pragma mark - Override
- (void)setText:(NSString *)text {
    [super setText:text];
    if (self.ty_paragraphStyle || self.ty_lineSpace > 0 || self.ty_wordSpace > 0) {
        self.ty_needRefreshParagraphStyle = YES;
    }
}

#pragma mark - Private
- (void)ty_updateParagraphStyle {
    NSMutableDictionary *attr = [NSMutableDictionary new];
    
    if (self.ty_wordSpace > 0) {
        [attr setValue:@(self.ty_wordSpace) forKey:NSKernAttributeName];
    }
    
    NSMutableParagraphStyle *paragraphStyle = self.ty_paragraphStyle;
    if (self.ty_lineSpace > 0) {
        if (paragraphStyle == nil) {
            paragraphStyle = [NSMutableParagraphStyle new];
            _ty_paragraphStyle = paragraphStyle;
        }
        if ([paragraphStyle isKindOfClass:[NSMutableParagraphStyle class]]) {
            paragraphStyle.lineSpacing = self.ty_lineSpace;
        }
    }
    if (paragraphStyle) {
        [attr setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
    }
    
    if (attr.count > 0 && [self.text isKindOfClass:[NSString class]]) {
        NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:self.text attributes:attr];
        self.attributedText = attrStr;
//        [self sizeToFit];
    }
    
    _ty_needRefreshParagraphStyle = NO;
}

- (void)drawTextInRect:(CGRect)rect {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.ty_edgeInsets)];
}

- (CGSize)intrinsicContentSize {
    CGSize size = [super intrinsicContentSize];
    size.width += self.ty_edgeInsets.left + self.ty_edgeInsets.right;
    size.height += self.ty_edgeInsets.top + self.ty_edgeInsets.bottom;
    return size;
}

- (CGSize)sizeThatFits:(CGSize)fitsSize {
    CGSize size = [super sizeThatFits:fitsSize];
    size.width += self.ty_edgeInsets.left + self.ty_edgeInsets.right;
    size.height += self.ty_edgeInsets.top + self.ty_edgeInsets.bottom;
    return size;
}

#pragma mark - Public
- (void)ty_refreshParagraphStyleImmediately {
    [self ty_updateParagraphStyle];
}

#pragma mark - Accessor
- (void)setTy_lineSpace:(CGFloat)ty_lineSpace {
    if (_ty_lineSpace != ty_lineSpace) {
        _ty_lineSpace = ty_lineSpace;
        if (self.text && self.text.length > 0) {
            self.ty_needRefreshParagraphStyle = YES;
        }
    }
}

- (void)setTy_wordSpace:(CGFloat)ty_wordSpace {
    if (_ty_wordSpace != ty_wordSpace) {
        _ty_wordSpace = ty_wordSpace;
        if (self.text && self.text.length > 0) {
            self.ty_needRefreshParagraphStyle = YES;
        }
    }
}

- (void)setTy_paragraphStyle:(NSMutableParagraphStyle *)ty_paragraphStyle {
    _ty_paragraphStyle = ty_paragraphStyle;
    if (self.text && self.text.length > 0) {
        self.ty_needRefreshParagraphStyle = YES;
    }
}

- (void)setty_edgeInsets:(UIEdgeInsets)ty_edgeInsets {
    if (!UIEdgeInsetsEqualToEdgeInsets(_ty_edgeInsets, ty_edgeInsets)) {
        _ty_edgeInsets = ty_edgeInsets;
        if (self.text && self.text.length > 0) {
            [self setNeedsDisplay];
        }
    }
}

#pragma mark - Refresh
- (void)setTy_needRefreshParagraphStyle:(BOOL)flag {
    if (self.ty_needRefreshParagraphStyle != flag) {
        _ty_needRefreshParagraphStyle = flag;
        if (flag) {
            __weak typeof(self) weakSelf = self;
//            dispatch_async(dispatch_get_main_queue(), ^{
                typeof(weakSelf) strongSelf = weakSelf;
                if (strongSelf.ty_needRefreshParagraphStyle) {
                    [strongSelf ty_updateParagraphStyle];
                }
//            });
        }
    }
}

@end
